

--[[
二八杠游戏规则
游戏使用一副除去大小王的扑克牌，共4个花色52张牌。
牌靴规则：
1. 40张（1-9筒 4张白板)
2. 白板0点
3. 每个位置发两张牌 
4. 牌型有 1散牌 2二八杠 3豹子 4金豹子
5. 相同的牌型有白板的大
6. 点数一样 比较最大牌
7. 都一样的牌庄赢

]]

local math_random = math.random
local table_copy = table.copy
local table_remove = table.remove
local table_mix = table.mix
local table_insert = table.insert
local string_split = string.split

local Rule = class("Rule")

function Rule:ctor()
	--随机种子
	math.randomseed(os.time())

	--牌说明 1-9筒子 0白板
	self.card = {
		0, 1, 2, 3, 4, 5, 6, 7, 8, 9
	}
	--牌型
	self.card_type = {
		high_card = 1, --散牌
		er_ba = 2, --二八杠		
		leopard = 3, --豹子
		gold_leopard = 4, --金豹		
	}
	--池位置
	self.pool_id = {		
		tian = 1,
		di = 2,
		ren = 3,	
		banker = 4,	
	}	
	self.cards = {} --当局可用的牌
	self.card_pair = 1	--有几副牌
	self.config_cards = nil --配置牌

end

--洗牌
function Rule:shuffle()	
	self.cards = {}
	for i = 1, self.card_pair do 
		for _, v in pairs(self.card) do 
			table.insert(self.cards, v)
		end
	end	
	table.mix(self.cards)
end

--获取点数
function Rule:getPoint(cards)
	local point = 0
	for k, v in ipairs(cards) do		
		point = v + point		
	end
	point = point % 10
	return point
end

--获取点数
function Rule:getCardType(cards)
	local point = 0
	local card1 = cards[1]
	local card2 = cards[2]
	if card1 == card2 then 
		if card1 == 0 then 
			return self.card_type.gold_leopard
		end
		return self.card_type.leopard
	end
	if (card1 == 2 and card2 == 8) or (card1 == 8 and card2 == 2) then 
		return self.card_type.er_ba
	end

	return self.card_type.high_card
end

--有白板
function Rule:hasBaiBan(cards)
	if cards[1] == 0 or cards[2] == 0 then 
		return true
	end
	return false
end

--取最大牌
function Rule:getMaxCard(cards)
	if cards[1] == 0 or cards[2] == 0 then 
		return 10
	end
	if cards[1] > cards[2] then 
		return cards[1]
	end
	return cards[2]
end

--牌一样
function Rule:isSameCards(cards1, cards2)
	if cards1[1] == cards2[1] and cards1[2] == cards2[2] then 
		return true
	end
	if cards1[1] == cards2[2] and cards1[2] == cards2[1] then 
		return true
	end
	return false
end

--比牌 cards1闲牌， cards2庄牌
function Rule:compareCard(cards1, cards2)
	-- print("______cards1__", cards1, cards1)
	if not next(cards1) or not next(cards2) then
		print("____1___比牌有错误了____")
		return -1
	end
	local cards1_win = 1 --玩家赢
	local cards2_win = 0 --庄赢
	local type1 = self:getCardType(cards1)
	local type2 = self:getCardType(cards2)
	if type1 > type2 then
		return cards1_win --闲赢
	elseif type1 < type2 then
		return cards2_win --庄赢
	end
	local point1 = self:getPoint(cards1)
	local point2 = self:getPoint(cards2)
	--牌型一样
	if type1 == self.card_type.high_card then --散牌
		if point1 > point2 then 
			return cards1_win
		elseif point1 < point2 then 
			return cards2_win
		end
		--牌一样庄赢
		if self:isSameCards(cards1, cards2) then 
			return cards2_win
		end
		--点数一样, 有白板的赢
		--点数一样 比较最大牌
		local max_card1 = self:getMaxCard(cards1)
		local max_card2 = self:getMaxCard(cards2)
		if max_card1 > max_card2 then 
			return cards1_win 
		end
		return cards2_win
		
	end
	
	if type1 == self.card_type.gold_leopard then --金豹
		return cards2_win
	end
	if type1 == self.card_type.leopard then --豹子
		if cards1[1] > cards2[1] then
			return cards1_win
		end
		return cards2_win
	end
	if type1 == self.card_type.er_ba then --二八杠
		return cards2_win
	end
	print("____2___比牌有错误了____",(cards1), (cards2))
end

--取 天 地 人 庄牌
function Rule:getCards()	
	if self.config_cards then 
		errlog("_config_cards___2222______", (self.config_cards))
		--有配置牌
		return self.config_cards
	end
	local cards = {
		{},
		{},
		{},
		{},
	}
	for k, v in pairs(cards) do
		table_insert(v, table_remove(self.cards, 1))			
		table_insert(v, table_remove(self.cards, 1))			
	end

	return cards
end


--配置牌
function Rule:configCards(uid, msg)
	print("______configCards______", uid, (msg))
	if msg.close == 0 then 
		self.config_cards = nil
		return 
	end
	local function setCards(cards)
		for k, v in pairs(cards) do 
			cards[k] = tonumber(cards[k])
		end
	end
	local cards1 = string_split(msg.cards1, ".")
	local cards2 = string_split(msg.cards2, ".")
	local cards3 = string_split(msg.cards3, ".")
	local cards4 = string_split(msg.cards4, ".")
	setCards(cards1)
	setCards(cards2)
	setCards(cards3)
	setCards(cards4)
	self.config_cards = {
		cards1,
		cards2,
		cards3,
		cards4,	
	}

	errlog("_config_cards_________", (self.config_cards))
	return true
end

--清空牌
function Rule:clearCard()
	self.cards = {}
end

--是否有配置牌
function Rule:isPeiPai()
	return self.config_cards
end



return Rule